function include_js(path) 
{     
	document.write("<scr"+"ipt language=javascript src='"+path+"'></scr"+"ipt>");
};
include_js("js/others/sylvester.js");
include_js("js/custom/Camera.js");
include_js("js/custom/Model.js");
include_js("js/custom/SandBoxControler.js");

function Room(){
	this.container=null;//div container
	this.stats=null;//small div on right-top to show stats
	this.scene=null;
	this.webglRenderer=null;
	
    this.group=null;
    
    this.roomCamera=null;
    this.roomModelManager=null;
    this.roomSandBoxControler=null;
    
    Room.self_ptr=this;
}
//static attribute
Room.self_ptr=null;
//prototype
Room.prototype = {
		constructor : Room,
		createModel:function(){
			//helper for:draw the cordinate system space
			var axisHelper=new THREE.AxisHelper();
			this.scene.add(axisHelper);
			//helper for:draw camera line
			//var cameraHelper=new THREE.CameraHelper(this.roomCamera.camera);
			//this.scene.add(cameraHelper);
			
			this.group = new THREE.Object3D();
			
			//Room
			var myRoom=new Model();
            myRoom.loadToGroup('models/room/newRoom.js', this.group);	
            myRoom.receiveShadow=true;
            this.roomModelManager.add(myRoom);
            
            //Pc
            var myPc=new Model();
            myPc.loadToGroup('models/apple_obj/apple_pc.js', this.group);	
            myPc.position.x=-20;
            myPc.position.y=6.5;
            myPc.castShadow=true;
            this.roomModelManager.add(myPc);
            //Apples
            var myApples=new Model();
            myApples.loadToGroup('models/my_apple_obj/apple.js',this.group);
            myApples.position.x=20;
            this.roomModelManager.add(myApples);
			//Bed 
            var myBed=new Model();
            myBed.loadToGroup('models/bed/bed1.js',this.group);
            myBed.castShadow=true;
            this.roomModelManager.add(myBed);
            
            //GlassTable
            var myGlassTable=new Model();
            myGlassTable.loadToGroup('models/glass_table/metal_glass_table.js', this.group);
            myGlassTable.transparent=true;
            myGlassTable.position.x=-20;
            myGlassTable.receiveShadow=true;
            this.roomModelManager.add(myGlassTable);
            
            //put the group in scene
            this.scene.add( this.group );
            
            this.roomSandBoxControler=new SandBoxControler(this.roomCamera);
            document.addEventListener( 'keydown', this.roomSandBoxControler.onDocumentKeyDown, false );
			document.addEventListener( 'keyup', this.roomSandBoxControler.onDocumentKeyUp, false );
		},
		lightInit:function(){
			 //ambientLight
			ambientLight = new THREE.AmbientLight( 0x000000 );
           	
			//spotLight
           	var spotLight = new THREE.SpotLight( 0xFFAA88, 2 );
           	spotLight.target.position.set( 0, 5, 0 );
           	spotLight.shadowCameraNear		= 0.01;		
			spotLight.position.set( -30, 20, 30 );
			spotLight.castShadow		= true;
			spotLight.shadowDarkness		= 0.5;
			spotLight.shadowCameraNear = 10;
			spotLight.shadowCameraFar = 200;
			spotLight.shadowCameraFov = 70;
			spotLight.shadowCameraVisible	= true;
            
			var spotLight2 = new THREE.SpotLight( 0xFFAA88, 1.5 );
           	spotLight2.target.position.set( 0, 10, 0 );
           	spotLight2.shadowCameraNear		= 0.01;		
			spotLight2.position.set( 0, -10, 0 );
			//spotLight2.castShadow		= true;
			spotLight2.shadowDarkness		= 0.5;
			spotLight2.shadowCameraNear = 10;
			spotLight2.shadowCameraFar = 50;
			spotLight2.shadowCameraFov = 50;
			//spotLight2.shadowCameraVisible	= true;
			//directionalLight
           directionalLight = new THREE.DirectionalLight( 0xffffff );
           directionalLight.intensity = 1.5;
           directionalLight.position.x = 10;
           directionalLight.position.y = 10;
           directionalLight.position.z = 13;
           directionalLight.position.normalize();
           directionalLight.shadowCameraVisible	= true;
           //put light in scene
          this.scene.add( ambientLight);
          this.scene.add(spotLight);
          this.scene.add(spotLight2);
          //this.scene.add(directionalLight);  
		},

        onStart:function(){
			
			//little stat window
			this.stats= new Stats();
			this.stats.domElement.style.position = 'absolute';
			this.stats.domElement.style.top = '0px';
			//set camera
			this.roomCamera=new Camera();
            this.roomCamera.init( 0, 10,40,new THREE.Vector3(0,8,0));
			//initial scene
			this.scene = new THREE.Scene();
			//
			this.lightInit();
			
			//initial webglRenderer
            this.webglRenderer = new THREE.WebGLRenderer();
            this.webglRenderer.setSize( window.innerWidth, window.innerHeight );
            this.webglRenderer.setFaceCulling( 0 );
            this.webglRenderer.shadowMapEnabled = true;//enable shadow 
            this.webglRenderer.shadowMapSoft = true;
			//create container and make it to the body
			this.container=document.createElement( 'div' );
			document.body.appendChild(this.container);
			//put the stats div and the canvas to the container
			this.container.appendChild( this.webglRenderer.domElement );
			this.container.appendChild( this.stats.domElement );		
			
			//new Model Manager
			this.roomModelManager=new ModelManager();
			
			//here will be change later
			//create model
		
			this.createModel();			
		},
    	onRender:function() {
            this.webglRenderer.render( this.scene, this.roomCamera.camera );
        },
		onUpdate:function() {
			this.stats.update();
            this.roomModelManager.update();
            this.roomSandBoxControler.update();
            this.roomCamera.update();
		},
		onAnimate:function() {
			requestAnimationFrame( Room.self_ptr.onAnimate );
			Room.self_ptr.onUpdate();
			Room.self_ptr.onRender();
		},
		
		excute:function(){
			this.onStart();
			this.onAnimate();
		}
};




	
